package com.it.park.util.image;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import com.it.park.util.file.FileUtil;

public class ImageUtil {
	
	/**
	 * 读取默认图像图片
	 */
	public static  byte[] getImageBytes(){
		byte[] buffer = null;
			InputStream is = ImageUtil.class.getResourceAsStream("/default.png");
			buffer = FileUtil.getBytes(is);
			return buffer;
	}
	
	/**
	 * 显示图片使用，根据二进制流
	 */
	public static void showPicture(byte[] data,HttpServletResponse response) throws IOException{
		response.setContentType("image/jpg");
		response.setCharacterEncoding("utf-8");
		OutputStream os = response.getOutputStream();
		InputStream is = new ByteArrayInputStream(data);
		int len = 0;
		byte[] b = new byte[1024];
		while((len = is.read(b)) != -1){
			os.write(b, 0, len);
		}
		os.close();
		is.close();
	}
	
	/**
	 * 显示图片使用，根据路径
	 */
	public static void showPicture(String path,HttpServletResponse response) throws IOException{
		byte[] data = FileUtil.getBytes(path);
		showPicture(data,response);
	}
	
}
